/**
 * 
 */
package com.wubo.sec.init;

import java.util.HashSet;
import java.util.Set;

import org.springframework.context.ApplicationContext;

import com.wubo.sec.SecurityManager;
import com.wubo.sec.core.RoleData;
import com.wubo.sec.model.Authority;
import com.wubo.sec.model.Group;
import com.wubo.sec.model.Menu;
import com.wubo.sec.model.Role;
import com.wubo.sec.model.User;
import com.wubo.sec.util.UUID;
import com.wubo.spring.hibernate.BaseHibernateDao;

/** 
 * 已过时，由{@link com.wubo.sec.init.XmlInitExecutor}代替
 * @author WuBo 
 * @CreateDate 2011-5-23
 * @version 1.0.1
 */
@Deprecated
public class ObjectInitExecutor extends SpringInitExecutor {

	public void doInit(ApplicationContext ctx){
		BaseHibernateDao dao = ctx.getBean(BaseHibernateDao.class);
		
		/********* 删除所有 *********/
		dao.executeSQL("delete from sec_user_role");
		dao.executeSQL("delete from sec_user_authority");
		dao.executeSQL("delete from sec_role_authority");
		dao.executeSQL("delete from sec_url_authority");
		dao.executeSQL("delete from sec_menu_authority");
		dao.executeSQL("delete from sec_user");
		dao.executeSQL("delete from sec_role");
		dao.executeSQL("delete from sec_authority");
		dao.executeSQL("delete from sec_urlresource");
		dao.executeSQL("delete from sec_menu");
		dao.executeSQL("delete from sec_group");
		dao.executeSQL("delete from sec_actionlog");
		dao.executeSQL("delete from sec_errorlog");
		
		/************************** 分隔线 ***************************/

		/* -- 创建权限 -- */
		//最高权限
		Set<Authority> authSet = new HashSet<Authority>(1);
		Authority auth = new Authority(Authority.ADMIN, "最高权限");
		auth.setType("SYSTEM");
		auth.setLocked(true);
		dao.save(auth);
		authSet.add(auth);

		//默认权限，每个用户都拥有
		Authority default_auth =Authority.DEFAULT_AUTH;
		dao.save(default_auth);
		
		//用户权限
		Set<Authority> userAuths = new HashSet<Authority>(4);
		userAuths.add( new Authority("USER_ADD", "增加用户", "用户", true) );
		userAuths.add( new Authority("USER_UPDATE", "更新用户", "用户", true) );
		userAuths.add( new Authority("USER_DELETE", "删除用户", "用户", true) );
		Authority userReadAuth = new Authority("USER_READ", "读取用户", "用户", true);
		userAuths.add( userReadAuth );
		userAuths.add( new Authority("USER_LOCK", "用户锁定", "用户", true) );
		userAuths.add( new Authority("USER_PASSWORD_RESET", "用户密码重置", "用户", true) );
		dao.saveOrUpdateAll(userAuths);
		
		//角色权限
		Set<Authority> roleAuths = new HashSet<Authority>(4);
		roleAuths.add( new Authority("ROLE_ADD", "增加角色", "角色", true) );
		roleAuths.add( new Authority("ROLE_UPDATE", "更新角色", "角色", true) );
		roleAuths.add( new Authority("ROLE_DELETE", "删除角色", "角色", true) );
		Authority roleReadAuth = new Authority("ROLE_READ", "读取角色", "角色", true);
		roleAuths.add( roleReadAuth );
		dao.saveOrUpdateAll(roleAuths);
		
		//用户-角色权限
		Set<Authority> userRoleAuths = new HashSet<Authority>(2);
		userRoleAuths.add( new Authority("USER_ROLE_RELATE", "关联用户角色", "用户角色分配", true) );
		Authority userRoleAuth = new Authority("USER_ROLE_READ", "查看用户角色", "用户角色分配", true);
		userRoleAuths.add(userRoleAuth);
		dao.saveOrUpdateAll(userRoleAuths);

		//权限权限
		Set<Authority> auths = new HashSet<Authority>(4);
		auths.add( new Authority("AUTH_ADD", "增加权限", "权限", true) );
		auths.add( new Authority("AUTH_UPDATE", "更新权限", "权限", true) );
		auths.add( new Authority("AUTH_DELETE", "删除权限", "权限", true) );
		Authority authReadAuth = new Authority("AUTH_READ", "读取权限", "权限", true);
		auths.add( authReadAuth );
		dao.saveOrUpdateAll(auths);
		
		//用户-权限权限
		Set<Authority> userAuthAuths = new HashSet<Authority>(2);
		userAuthAuths.add( new Authority("USER_AUTH_RELATE", "关联用户权限", "用户权限分配", true) );
		Authority userAuthAuth = new Authority("USER_AUTH_READ", "查看用户权限", "用户权限分配", true);
		userAuthAuths.add(userAuthAuth);
		dao.saveOrUpdateAll(userAuthAuths);
		
		//角色-权限权限
		Set<Authority> roleAuthAuths = new HashSet<Authority>(2);
		roleAuthAuths.add( new Authority("ROLE_AUTH_RELATE", "关联角色权限", "角色权限", true) );
		Authority roleAuthAuth = new Authority("ROLE_AUTH_READ", "查看角色权限", "角色权限", true);
		roleAuthAuths.add(roleAuthAuth);
		dao.saveOrUpdateAll(roleAuthAuths);

		//菜单权限
		Set<Authority> menuAuths = new HashSet<Authority>(4);
		menuAuths.add( new Authority("MENU_ADD", "增加菜单", "菜单", true) );
		menuAuths.add( new Authority("MENU_UPDATE", "更新菜单", "菜单", true) );
		menuAuths.add( new Authority("MENU_DELETE", "删除菜单", "菜单", true) );
		Authority menuReadAuth = new Authority("MENU_READ", "读取菜单", "菜单", true);
		menuAuths.add( menuReadAuth );
		dao.saveOrUpdateAll(menuAuths);
		
		//菜单-权限权限
		Set<Authority> menuAuthAuths = new HashSet<Authority>(2);
		menuAuthAuths.add( new Authority("MENU_AUTH_RELATE", "关联菜单权限", "菜单权限分配", true) );
		Authority menuAuthAuth = new Authority("MENU_AUTH_READ", "查看菜单权限", "菜单权限分配", true);
		menuAuthAuths.add(menuAuthAuth);
		dao.saveOrUpdateAll(menuAuthAuths);

		//用户组权限
		Set<Authority> groupAuths = new HashSet<Authority>(4);
		groupAuths.add( new Authority("GROUP_ADD", "增加用户组", "用户组", true) );
		groupAuths.add( new Authority("GROUP_UPDATE", "更新用户组", "用户组", true) );
		groupAuths.add( new Authority("GROUP_DELETE", "删除用户组", "用户组", true) );
		Authority groupReadAuth = new Authority("GROUP_READ", "读取用户组", "用户组", true);
		groupAuths.add( groupReadAuth );
		dao.saveOrUpdateAll(groupAuths);
		
		//用户-用户组权限
		Set<Authority> userGroupAuths = new HashSet<Authority>(2);
		userGroupAuths.add( new Authority("USER_GROUP_RELATE", "关联用户用户组", "用户组分配", true) );
		Authority userGroupAuth = new Authority("USER_GROUP_READ", "查看用户用户组", "用户组分配", true);
		userGroupAuths.add(userGroupAuth);
		dao.saveOrUpdateAll(userGroupAuths);
		
		//URL资源权限
		Set<Authority> urlAuths = new HashSet<Authority>(4);
		urlAuths.add( new Authority("URLRESOURCE_ADD", "增加URL资源", "URL", true) );
		urlAuths.add( new Authority("URLRESOURCE_UPDATE", "更新URL资源", "URL", true) );
		urlAuths.add( new Authority("URLRESOURCE_DELETE", "删除URL资源", "URL", true) );
		Authority urlReadAuth = new Authority("URLRESOURCE_READ", "读取URL资源", "URL", true);
		urlAuths.add( urlReadAuth );
		dao.saveOrUpdateAll(urlAuths);
		
		//URL资源权限权限
		Set<Authority> urlAuthAuths = new HashSet<Authority>(2);
		urlAuthAuths.add( new Authority("URL_AUTH_RELATE", "关联URL权限", "URL权限分配", true) );
		Authority urlAuthAuth = new Authority("URL_AUTH_READ", "查看URL权限", "URL权限分配", true);
		urlAuthAuths.add(urlAuthAuth);
		dao.saveOrUpdateAll(urlAuthAuths);
		
		//日志权限
		Set<Authority> logAuths = new HashSet<Authority>(4);
		logAuths.add( new Authority("ERRORLOG_CLEAR", "清除错误日志", "日志", true) );
		logAuths.add( new Authority("ACTIONLOG_DELETE", "删除操作日志", "日志", true) );
		Authority errorlogReadAuth = new Authority("ERRORLOG_READ", "读取错误日志", "日志", true);
		Authority actionlogReadAuth = new Authority("ACTIONLOG_READ", "读取操作日志", "日志", true);
		logAuths.add(errorlogReadAuth);
		logAuths.add(actionlogReadAuth);
		dao.saveOrUpdateAll(logAuths);
		
		/* -- 创建用户组 -- */
		Group group = new Group();
		group.setCode("ADMINISTRATOR");
		group.setMark("ADMIN");
		group.setMarkLocked(false);
		group.setName("系统管理组");
		group.setPriority(Group.SUPER);
		
		dao.save(group); //创建系统管理组
		dao.save(Group.DEFAULT_GROUP); //创建默认组
		
		/* -- 创建角色 -- */
		Role adminRole = new Role();
		adminRole.setCode(UUID.uuid());
		adminRole.setRoleName("系统管理员");
		adminRole.setCreateTime(new java.util.Date());
		adminRole.setLocked(true);
		adminRole.setAuthorities(authSet);
		dao.save(adminRole);
		
		Role userRole = new Role();
		userRole.setCode(UUID.uuid());
		userRole.setRoleName("用户管理员");
		userRole.setLocked(true);
		userRole.setCreateTime(new java.util.Date());
		userRole.setAuthorities(userAuths);
		dao.save(userRole);
		
		Role roleRole = new Role();
		roleRole.setCode(UUID.uuid());
		roleRole.setRoleName("角色管理员");
		roleRole.setLocked(true);
		roleRole.setCreateTime(new java.util.Date());
		roleRole.setAuthorities(roleAuths);
		dao.save(roleRole);
		
		Role groupRole = new Role();
		groupRole.setCode(UUID.uuid());
		groupRole.setRoleName("用户组管理员");
		groupRole.setLocked(true);
		groupRole.setCreateTime(new java.util.Date());
		groupRole.setAuthorities(groupAuths);
		dao.save(groupRole);
		
		Role menuRole = new Role();
		menuRole.setCode(UUID.uuid());
		menuRole.setRoleName("菜单管理员");
		menuRole.setLocked(true);
		menuRole.setCreateTime(new java.util.Date());
		menuRole.setAuthorities(menuAuths);
		dao.save(menuRole);

		Role userRoleRole = new Role();
		userRoleRole.setCode(UUID.uuid());
		userRoleRole.setRoleName("用户角色管理员");
		userRoleRole.setLocked(true);
		userRoleRole.setCreateTime(new java.util.Date());
		userRoleRole.setAuthorities(userRoleAuths);
		dao.save(userRoleRole);
		
		Role userAuthRole = new Role();
		userAuthRole.setCode(UUID.uuid());
		userAuthRole.setRoleName("用户权限管理员");
		userAuthRole.setLocked(true);
		userAuthRole.setCreateTime(new java.util.Date());
		userAuthRole.setAuthorities(userAuthAuths);
		dao.save(userAuthRole);
		
		Role roleAuthRole = new Role();
		roleAuthRole.setCode(UUID.uuid());
		roleAuthRole.setRoleName("角色权限管理员");
		roleAuthRole.setLocked(true);
		roleAuthRole.setCreateTime(new java.util.Date());
		roleAuthRole.setAuthorities(roleAuthAuths);
		dao.save(roleAuthRole);
		
		Role menuAuthRole = new Role();
		menuAuthRole.setCode(UUID.uuid());
		menuAuthRole.setRoleName("菜单权限管理员");
		menuAuthRole.setLocked(true);
		menuAuthRole.setCreateTime(new java.util.Date());
		menuAuthRole.setAuthorities(menuAuthAuths);
		dao.save(menuAuthRole);
		
		Role urlRole = new Role();
		urlRole.setCode(UUID.uuid());
		urlRole.setRoleName("URL资源管理员");
		urlRole.setLocked(true);
		urlRole.setCreateTime(new java.util.Date());
		urlRole.setAuthorities(urlAuths);
		dao.save(urlRole);

		Role authRole = new Role();
		authRole.setCode(UUID.uuid());
		authRole.setRoleName("权限管理员");
		authRole.setLocked(true);
		authRole.setCreateTime(new java.util.Date());
		authRole.setAuthorities(auths);
		dao.save(authRole);
		
		Role logRole = new Role();
		logRole.setCode(UUID.uuid());
		logRole.setRoleName("日志管理员");
		logRole.setLocked(true);
		logRole.setCreateTime(new java.util.Date());
		logRole.setAuthorities(logAuths);
		dao.save(logRole);
		
		/* -- 创建菜单 -- */
		boolean isFilter = false; //是否参与权限过滤开关
		
		Menu system = new Menu();
		system.setCode(UUID.uuid());
		system.setGrade(1);
		system.setOrderIndex(1);
		system.setLeaf(false);
		system.setName("系统管理");
		system.setLocked(true);
		system.setFilter(isFilter);
		dao.save(system);
		
		Menu userMenu = new Menu();
		userMenu.setCode(UUID.uuid());
		userMenu.setGrade(2);
		userMenu.setOrderIndex(1);
		userMenu.setLeaf(true);
		userMenu.setName("用户管理");
		userMenu.setUrl("/protected/system/listUser.action");
		userMenu.setParentCode(system.getCode());
		userMenu.setLocked(true);
		userMenu.setAuth(userReadAuth);
		userMenu.setFilter(isFilter);
		dao.save(userMenu);
		
		Menu roleMenu = new Menu();
		roleMenu.setCode(UUID.uuid());
		roleMenu.setGrade(2);
		roleMenu.setOrderIndex(2);
		roleMenu.setLeaf(true);
		roleMenu.setName("角色管理");
		roleMenu.setUrl("/protected/system/listRole.action");
		roleMenu.setParentCode(system.getCode());
		roleMenu.setLocked(true);
		roleMenu.setAuth(roleReadAuth);
		roleMenu.setFilter(isFilter);
		dao.save(roleMenu);
		
		Menu userRoleMenu = new Menu();
		userRoleMenu.setCode(UUID.uuid());
		userRoleMenu.setGrade(2);
		userRoleMenu.setOrderIndex(3);
		userRoleMenu.setLeaf(true);
		userRoleMenu.setName("用户角色关联");
		userRoleMenu.setUrl("/protected/system/userrolemgt.jsp");
		userRoleMenu.setParentCode(system.getCode());
		userRoleMenu.setLocked(true);
		userRoleMenu.setAuth(userRoleAuth);
		userRoleMenu.setFilter(isFilter);
		dao.save(userRoleMenu);
		
		Menu authMenu = new Menu();
		authMenu.setCode(UUID.uuid());
		authMenu.setGrade(2);
		authMenu.setOrderIndex(4);
		authMenu.setLeaf(true);
		authMenu.setName("权限管理");
		authMenu.setUrl("/protected/system/listAuth.action");
		authMenu.setParentCode(system.getCode());
		authMenu.setLocked(true);
		authMenu.setAuth(authReadAuth);
		authMenu.setFilter(isFilter);
		dao.save(authMenu);
		
		Menu userAuthMenu = new Menu();
		userAuthMenu.setCode(UUID.uuid());
		userAuthMenu.setGrade(2);
		userAuthMenu.setOrderIndex(5);
		userAuthMenu.setLeaf(true);
		userAuthMenu.setName("用户权限关联");
		userAuthMenu.setUrl("/protected/system/userauthmgt.jsp");
		userAuthMenu.setParentCode(system.getCode());
		userAuthMenu.setLocked(true);
		userAuthMenu.setAuth(userAuthAuth);
		userAuthMenu.setFilter(isFilter);
		dao.save(userAuthMenu);
		
		Menu roleAuthMenu = new Menu();
		roleAuthMenu.setCode(UUID.uuid());
		roleAuthMenu.setGrade(2);
		roleAuthMenu.setOrderIndex(6);
		roleAuthMenu.setLeaf(true);
		roleAuthMenu.setName("角色权限关联");
		roleAuthMenu.setUrl("/protected/system/roleauthmgt.jsp");
		roleAuthMenu.setParentCode(system.getCode());
		roleAuthMenu.setLocked(true);
		roleAuthMenu.setAuth(roleAuthAuth);
		roleAuthMenu.setFilter(isFilter);
		dao.save(roleAuthMenu);
		
		Menu menuMenu = new Menu();
		menuMenu.setCode(UUID.uuid());
		menuMenu.setGrade(2);
		menuMenu.setOrderIndex(7);
		menuMenu.setLeaf(true);
		menuMenu.setName("菜单管理");
		menuMenu.setUrl("/protected/system/listMenu.action");
		menuMenu.setParentCode(system.getCode());
		menuMenu.setLocked(true);
		menuMenu.setAuth(menuReadAuth);
		menuMenu.setFilter(isFilter);
		dao.save(menuMenu);
		
		Menu menuAuthMenu = new Menu();
		menuAuthMenu.setCode(UUID.uuid());
		menuAuthMenu.setGrade(2);
		menuAuthMenu.setOrderIndex(8);
		menuAuthMenu.setLeaf(true);
		menuAuthMenu.setName("菜单权限关联");
		menuAuthMenu.setUrl("/protected/system/menuauthmgt.jsp");
		menuAuthMenu.setParentCode(system.getCode());
		menuAuthMenu.setLocked(true);
		menuAuthMenu.setAuth(menuAuthAuth);
		menuAuthMenu.setFilter(isFilter);
		dao.save(menuAuthMenu);
		
		Menu groupMenu = new Menu();
		groupMenu.setCode(UUID.uuid());
		groupMenu.setGrade(2);
		groupMenu.setOrderIndex(9);
		groupMenu.setLeaf(true);
		groupMenu.setName("用户组管理");
		groupMenu.setUrl("/protected/system/listGroup.action");
		groupMenu.setParentCode(system.getCode());
		groupMenu.setLocked(true);
		groupMenu.setAuth(groupReadAuth);
		groupMenu.setFilter(isFilter);
		dao.save(groupMenu);
		
		Menu userGroupMenu = new Menu();
		userGroupMenu.setCode(UUID.uuid());
		userGroupMenu.setGrade(2);
		userGroupMenu.setOrderIndex(10);
		userGroupMenu.setLeaf(true);
		userGroupMenu.setName("用户用户组关联");
		userGroupMenu.setUrl("/protected/system/usergroupmgt.jsp");
		userGroupMenu.setParentCode(system.getCode());
		userGroupMenu.setLocked(true);
		userGroupMenu.setAuth(userGroupAuth);
		userGroupMenu.setFilter(isFilter);
		dao.save(userGroupMenu);
		
		Menu urlMenu = new Menu();
		urlMenu.setCode(UUID.uuid());
		urlMenu.setGrade(2);
		urlMenu.setOrderIndex(11);
		urlMenu.setLeaf(true);
		urlMenu.setName("URL资源管理");
		urlMenu.setUrl("/protected/system/listUrl.action");
		urlMenu.setParentCode(system.getCode());
		urlMenu.setLocked(true);
		urlMenu.setAuth(urlReadAuth);
		urlMenu.setFilter(isFilter);
		dao.save(urlMenu);
		
		Menu urlAuthMenu = new Menu();
		urlAuthMenu.setCode(UUID.uuid());
		urlAuthMenu.setGrade(2);
		urlAuthMenu.setOrderIndex(11);
		urlAuthMenu.setLeaf(true);
		urlAuthMenu.setName("URL资源权限关联");
		urlAuthMenu.setUrl("/protected/system/urlauthmgt.jsp");
		urlAuthMenu.setParentCode(system.getCode());
		urlAuthMenu.setLocked(true);
		urlAuthMenu.setAuth(urlAuthAuth);
		urlAuthMenu.setFilter(isFilter);
		dao.save(urlAuthMenu);
		
		Menu actionLogMenu = new Menu();
		actionLogMenu.setCode(UUID.uuid());
		actionLogMenu.setGrade(2);
		actionLogMenu.setOrderIndex(12);
		actionLogMenu.setLeaf(true);
		actionLogMenu.setName("操作日志");
		actionLogMenu.setUrl("/protected/system/listActionLog.action");
		actionLogMenu.setParentCode(system.getCode());
		actionLogMenu.setLocked(true);
		actionLogMenu.setAuth(actionlogReadAuth);
		actionLogMenu.setFilter(isFilter);
		dao.save(actionLogMenu);
		
		Menu errorLogMenu = new Menu();
		errorLogMenu.setCode(UUID.uuid());
		errorLogMenu.setGrade(2);
		errorLogMenu.setOrderIndex(13);
		errorLogMenu.setLeaf(true);
		errorLogMenu.setName("错误日志");
		errorLogMenu.setUrl("/protected/system/listErrorLog.action");
		errorLogMenu.setParentCode(system.getCode());
		errorLogMenu.setLocked(true);
		errorLogMenu.setAuth(errorlogReadAuth);
		errorLogMenu.setFilter(isFilter);
		dao.save(errorLogMenu);
		
		//个人设定
		Menu configMenu = new Menu();
		configMenu.setCode(UUID.uuid());
		configMenu.setOrderIndex(2);
		configMenu.setLeaf(false);
		configMenu.setName("个人设定");
		configMenu.setLocked(true);
		configMenu.setFilter(isFilter);
		dao.save(configMenu);
		
		Menu changePwd = new Menu();
		changePwd.setCode(UUID.uuid());
		changePwd.setGrade(2);
		changePwd.setOrderIndex(1);
		changePwd.setLeaf(true);
		changePwd.setName("修改密码");
		changePwd.setUrl("/protected/system/changepwd.jsp");
		changePwd.setParentCode(configMenu.getCode());
		changePwd.setLocked(true);
		changePwd.setFilter(isFilter);
		changePwd.setAuth(default_auth);
		dao.save(changePwd);
		
		/* -- 创建用户 -- */
		User user = new User();
		user.setActive(true);
		user.setUsername("admin");
		user.setCreateTime(new java.util.Date());
		user.setPassword(SecurityManager.encodePassword("admin", "admin"));
		user.setTruename("系统管理员");
		user.setGroup(group);
		Set<RoleData> roles = new HashSet<RoleData>();
		roles.add(adminRole);
		user.setRoles(roles);
		dao.save(user);
	}
}
